<html>
	<head>
		<title>CFHibernate Dynamic Map Testing App</title>
	</head>
	<body>
	
	
<cfscript>
// get Hibernate session factory
sessionFactory = Application.hibernateConfig.buildSessionFactory();

// open a session and start the transaction
mySession = sessionFactory.openSession();
EntityMode = createObject("java", "org.hibernate.EntityMode");
mySession = mySession.getSession(EntityMode.MAP);

/*
tx = mySession.beginTransaction();

user = createObject("java", "java.util.HashMap").init();
user.put("firstName", "Dynamic");
user.put("lastName", "Testing");
user.put("email", "hibernate@test.com");

address = createObject("java", "java.util.HashMap").init();
address.put("city", "Cottage Grove");
address.put("state", "MN");
address.put("zip", "55016");

addresses = createObject("java", "java.util.HashSet").init();
addresses.add(address);
user.put("addresses", addresses);

mySession.saveOrUpdate("Address", address);
mySession.saveOrUpdate("User", user);

tx.commit();
*/

user = mySession.createQuery("from User user where userID = 1").uniqueResult();

//mySession.close();
</cfscript>

<cfdump var="#user.toString()#" label="user.toString()">

<p>
<cfoutput>
<cfloop collection="#user#" item="key">
	<cfif isDefined("user.#key#")>
		<cfif user[key].getClass().getName() neq "org.hibernate.collection.PersistentSet">
			#key# = #user[key]# "#user[key].getClass().getName()#"<br />
		<cfelse>
			#key# = Set [
			<cfset theset = user[key]>
			<cfset iterator = theSet.iterator()>
			<cfloop condition="#iterator.hasNext()#">
				{
				<cfset element = iterator.next()>
				<cfif isStruct(element)>
					<cfloop collection="#element#" item="key2">
						#key2# = <cfif isDefined('element.#key2#')>#element[key2]#<cfelse>NULL</cfif><br />
					</cfloop>
				<cfelse>
					#key2# = #element[key2]#<br />
				</cfif>
				}
			</cfloop> ]<br />
		</cfif>
	<cfelse>
		#key# = NOT DEFINED<br />
	</cfif>
</cfloop>
</cfoutput>
</p>

<cffunction name="mapToCFC" returntype="any">
	<cfargument name="data" type="struct" required="true">
	<cfset var key = 0>
	<cfset var element = 0>
	<cfset var array = 0>
	<cfset var result = 0>
	<cfset var resultData = structNew()>
	<cfset var theSet = 0>
	<cfset var iterator = 0>
	<cfloop collection="#data#" item="key">
		<cfif isDefined("data.#key#")>
			<cfif data[key].getClass().getName() neq "org.hibernate.collection.PersistentSet">
				<cfset resultData[key] = data[key]>
			<cfelse>
				<cfset array = arrayNew(1)>
				<cfset theset = data[key]>
				<cfset iterator = theSet.iterator()>
				<cfloop condition="#iterator.hasNext()#">
					<cfset element = iterator.next()>
					<cfif isStruct(element)>
						<cfset arrayAppend(array, mapToCFC(element))>
					<cfelse>
						<cfset arrayAppend(array, element)>
					</cfif>
				</cfloop>
				<cfset resultData[key] = array>
			</cfif>
		</cfif>
	</cfloop>
	<cfset result = createObject("component", data["$type$"]).init(argumentCollection=resultData)>
	<cfreturn result>
</cffunction>

<cffunction name="mapToHashMap" returntype="struct" access="private" output="false">
	<cfargument name="result" type="any" required="true" />
	<cfset var hashMap = CreateObject("java", "java.util.HashMap").init()>
	<cfset var props = result.getTO()>
	<cfset var prop = 0>
	<cfset var i = 0>
	<cfloop list="#structKeyList(props)#" index="prop">
		<cfif isObject(evaluate('props.#prop#'))>
			<cfset hashMap["#prop#"] = mapToHashMap(evaluate('props.#prop#'))>
		<cfelseif isArray(evaluate('props.#prop#'))>
			<cfset hashMap["#prop#"] = createObject("java", "java.util.HashSet").init()>
			<cfloop from="1" to="#arrayLen(evaluate('props.#prop#'))#" index="i">
				<cfif isObject(evaluate('props.#prop#[#i#]'))>
					<cfset hashMap["#prop#"].add(mapToHashMap(evaluate('props.#prop#[#i#]')))>
				<cfelse>
					<cfset hashMap["#prop#"].add(evaluate('props.#prop#[#i#]'))>
				</cfif>
			</cfloop>
		<cfelse>
			<cfset hashMap["#prop#"] = evaluate('props.#prop#')>
		</cfif>
	</cfloop>
	<cfset hashMap["$type$"] = getMetaData(result).name>
	<cfreturn hashMap>
</cffunction>

<cfset userCFC = mapToCFC(user)>		

<cfdump var="#userCFC.getInstanceMemento()#" label="userCFC">

<cfset mySession.close()>

<cfoutput>
<p>#mapToHashMap(userCFC).toString()#</p>

<p>#userCFC.getInstanceMemento().keySet().toString()#></p>

</cfoutput>

	</body>
</html>